#include "SDeviceGpib.h"
#include "SDeviceError.h"
#include <qdebug>

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDeviceGpib::gpibSend(QString commande)
///
/// \brief	send a direct commande to this device by gbib. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \param	commande	The commande sent to the device. 
///
/// \remark this function is normally used only by the other functions of the class, 
/// 		not by the user. It do not check anything So use this function carefully. 
////////////////////////////////////////////////////////////////////////////////////////////////////

void SDeviceGpib::gpibSend(QString commande){
	if (!available_) throw SDEVICEERROR_GPIB_DEVICE_UNAVAILABLE;

	try{	
		gpib_->send(elmt_, commande);
		SDeviceVirtual::addLog(QString("sent commande : ") + commande);
	}catch(SError error){
		qDebug() << "ERROR :" << error;
		addLog(QString("ERROR : ") + error);

	}
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	QVariant SDeviceGpib::gpibRead(const long longBuffer)const
///
/// \brief	Read data in the GPIB board. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \param	longBuffer	length of the buffer, by default 131072 bits (128 Ko).
///
/// \return	. 
///
/// \remark this function is normally used only by the other functions of the class, 
/// 		not by the user. It do not check anything So use this function carefully.
////////////////////////////////////////////////////////////////////////////////////////////////////

QVariant SDeviceGpib::gpibRead(const long buffer) { 
	if (!available_) throw SDEVICEERROR_GPIB_DEVICE_UNAVAILABLE;

	try{	
		
		addLog(QString("read data : buffer = ") + QString(buffer));
		return gpib_->receive(elmt_,buffer);
	}catch(QString error){
		qDebug() << "ERROR :" << error;
		addLog(QString("ERROR : ") + error);
	}

	return QVariant("");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDeviceGpib::setGpibElement(const SInterfaceGpib::GpibElement elmt)
///
/// \brief	Sets a GPIB element which contains the contains the name, the primary and secondary address. . 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \param	elmt GPIB element. 
/// 			 
/// \remarks	Normaly the primary adress is auto set at the construction of the object. So use
/// 			this function carefully. 
////////////////////////////////////////////////////////////////////////////////////////////////////

void SDeviceGpib::setGpibElement(const SInterfaceGpib::GpibElement elmt){

	Q_ASSERT(elmt.PAD <=30 && elmt.PAD >0);
	
	addLog(QString("GpibElement changed : ") 
				+ QString(" Name : ")  + elmt.name
				+ QString(", PAD : ")  + elmt.PAD 
				+ QString(", SAD : ")  + elmt.SAD 
				+ QString(", mode : ")  + elmt.mode);
	elmt_ = elmt;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	Addr4882_t SDeviceGpib::PAD(void) const
///
/// \brief	Gets the PAD (Primary address) of the device. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \return	the PAD (Primary address). 
///
////////////////////////////////////////////////////////////////////////////////////////////////////

Addr4882_t SDeviceGpib::PAD(void) const{

	return elmt_.PAD;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	SInterfaceGpib::GpibElement SDeviceGpib::GpibElement(void) const
///
/// \brief	Gets the GPIB element which contains the contains the name, the primary and secondary address. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \return	the GPIB element. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////

SInterfaceGpib::GpibElement SDeviceGpib::GpibElement(void) const
{
	return elmt_;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDeviceGpib::setPAD(const Addr4882_t pad)
///
/// \brief	Sets a PAD (Primary address) of the device. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \param	pad	The pad. 
///
/// \remarks	Normaly the primary adress is auto set at the construction of the object. So use
/// 			this function carefully. 
////////////////////////////////////////////////////////////////////////////////////////////////////

void SDeviceGpib::setPAD(const Addr4882_t pad){
	Q_ASSERT(pad <=30 && pad >0);
	elmt_.PAD = pad;

	addLog(QString("PAD changed : ") + pad);

}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	SInterfaceGpib* SDeviceGpib::GpibInterface(void) const
///
/// \brief	Gets the gpib interface. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \return	a pointer to the GpibInterface.
/// 		
/// \remark this function is normally used only by the other functions of the class, 
/// 		not by the user. It do not check anything So use this function carefully.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

SInterfaceGpib* SDeviceGpib::GpibInterface(void) const
{
	return gpib_;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	QString SDeviceGpib::name(void) const
///
/// \brief	Gets the name. 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
/// \return	The efective name of the device. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////

QString SDeviceGpib::name(void) const{
	return elmt_.name;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDeviceGpib::enableLocal(void)
///
/// \brief	actives the front panel commands (activates the "local mode"). 
///
/// \author	Sylvain Martin
/// \date	1/13/2011
///
////////////////////////////////////////////////////////////////////////////////////////////////////

void SDeviceGpib::enableLocal(void){
	gpib_->enableLocal(elmt_.PAD);
	
}

